from django.urls import path
import re

from django import forms
from django.contrib.auth import authenticate, login
from django.core.cache import cache
from django.utils.timezone import now
from django.db import transaction

from rec.models import Profile, RecProfileOut, Exporsure, HotItem
from utils import constants
import json

class UploadForm(forms.Form):
    # """ 用户注册 """
    # appId = forms.CharField(label='APP', max_length=64, required=True, error_messages={
    #     'required': '请输入appid'
    # })
    # userId = forms.CharField(label='用户', max_length=32, required=True, error_messages={
    #     'required': '请输入userid'
    # })
    # objectId = forms.CharField(label='商品', max_length=32, required=True, error_messages={
    #     'required': '请输入objectId'
    # })
    # objectType = forms.CharField(label='商品类型', max_length=32, required=True, error_messages={
    #     'required': '请输入objectType'
    # })
    # motion = forms.CharField(label='用户行为', max_length=32, required=True, error_messages={
    #     'required': '请输入motion'
    # })
    # visitTime = forms.CharField(label='行为时间', max_length=32, required=True, error_messages={
    #     'required': '请输入visitTime'
    # })


    @transaction.atomic
    def do_update(self, request):
        """ 执行注册 """
        data = json.loads(request.body)['tracks']
        print(data)
        # source=request.headers.get('source', '')
        try:
            for item in data:
                profile = Profile.objects.create(
                    appId=item.get('appId', None),
                    userId=item.get('userId', None),
                    objectId=item.get('objectId', None),
                    objectType=item.get('objectType', None),
                    motion=item.get('motion', None),
                    visitTime=item.get('visitTime', None),
                )
            print(profile)
            return profile
        except Exception as e:
            print(e)
            return None

class ResultForm(forms.Form):

    @transaction.atomic
    def do_result(self, request):
        """ 执行注册 """
        data = json.loads(request.body)
        print(data)

        try:
            return data
        except Exception as e:
            print(e)
            return None

class HotForm(forms.Form):

    @transaction.atomic
    def do_hot(self, request):
        """ 执行注册 """
        data = json.loads(request.body)
        print(data)

        try:
            return data
        except Exception as e:
            print(e)
            return None

class OutcomeForm(forms.Form):

    @transaction.atomic
    def do_outcome(self, request):
        """ 执行注册 """
        profile = json.loads(request.body)
        print(profile, len(profile['tracks']), len(profile['exposures']), len(profile['hotItems']))

        # source=request.headers.get('source', '')
        try:
            if len(profile['tracks']) > 0:
                data_track = json.loads(request.body)['tracks']
                for item in data_track:
                    item_scores = item['recItems']
                    # print(item_scores)
                    if len(item_scores) < 10:
                        item_scores.extend([{'objectId': None, 'score': None} for x in range(10-len(item_scores))])
                    profile_out = RecProfileOut.objects.create(
                        appId=item.get('appId', None),
                        userId=item.get('userId', None),
                        objectId=item.get('objectId', None),
                        objectType=item.get('objectType', None),
                        motion=item.get('motion', None),
                        visitTime=item.get('visitTime', None),
                        location=item.get('location', None),
                        method=item.get('method', None),
                        item1=item_scores[0].get('objectId', None),
                        score1=item_scores[0].get('score', None),
                        item2=item_scores[1].get('objectId', None),
                        score2=item_scores[1].get('score', None),
                        item3=item_scores[2].get('objectId', None),
                        score3=item_scores[2].get('score', None),
                        item4=item_scores[3].get('objectId', None),
                        score4=item_scores[3].get('score', None),
                        item5=item_scores[4].get('objectId', None),
                        score5=item_scores[4].get('score', None),
                        item6=item_scores[5].get('objectId', None),
                        score6=item_scores[5].get('score', None),
                        item7=item_scores[6].get('objectId', None),
                        score7=item_scores[6].get('score', None),
                        item8=item_scores[7].get('objectId', None),
                        score8=item_scores[7].get('score', None),
                        item9=item_scores[8].get('objectId', None),
                        score9=item_scores[8].get('score', None),
                        item10=item_scores[9].get('objectId', None),
                        score10=item_scores[9].get('score', None),
                    )
            if len(profile['exposures']) > 0:
                data_exposure = json.loads(request.body)['exposures']
                for it in data_exposure:
                    profile_out = Exporsure.objects.create(
                        appId=it.get('appId', None),
                        userId=it.get('userId', None),
                        visitTime=it.get('visitTime', None),
                        location=it.get('location', None),
                    )

            if len(profile['hotItems']) > 0:
                data_hot = json.loads(request.body)['hotItems']
                item_scores = data_hot['items']
                print(item_scores)
                if len(item_scores) < 10:
                    item_scores.extend([{'objectId': None, 'score': None} for x in range(10-len(item_scores))])
                profile_out = HotItem.objects.create(
                    appId=data_hot.get('appId', None),
                    item1=item_scores[0].get('objectId', None),
                    score1=item_scores[0].get('score', None),
                    item2=item_scores[1].get('objectId', None),
                    score2=item_scores[1].get('score', None),
                    item3=item_scores[2].get('objectId', None),
                    score3=item_scores[2].get('score', None),
                    item4=item_scores[3].get('objectId', None),
                    score4=item_scores[3].get('score', None),
                    item5=item_scores[4].get('objectId', None),
                    score5=item_scores[4].get('score', None),
                    item6=item_scores[5].get('objectId', None),
                    score6=item_scores[5].get('score', None),
                    item7=item_scores[6].get('objectId', None),
                    score7=item_scores[6].get('score', None),
                    item8=item_scores[7].get('objectId', None),
                    score8=item_scores[7].get('score', None),
                    item9=item_scores[8].get('objectId', None),
                    score9=item_scores[8].get('score', None),
                    item10=item_scores[9].get('objectId', None),
                    score10=item_scores[9].get('score', None),
                )
            return profile
        except Exception as e:
            print(e)
            return None